package com.example.zyfypt516wh.model;

import static com.example.zyfypt516wh.common.Common.BASEURL;

import com.example.zyfypt516wh.iface.RegisterListener;
import com.example.zyfypt516wh.iface.Registeriface;
import com.example.zyfypt516wh.service.UserService;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public class RegisterModel implements Registeriface {
    private Retrofit retrofit;

    public RegisterModel()
    {
        retrofit = new Retrofit.Builder().
                baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }



    @Override
    public void getRegisterResult(String username, String pass, String tel, final RegisterListener registerListener) {
        UserService userService = retrofit.create(UserService.class);
        Call<String> call = userService.register(username,pass,tel);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && response!=null)
                {
                    registerListener.onResponse(response.body());
                }
                else registerListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                registerListener.onFail(t.toString());
            }
        });
    }
}
